﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Enferno.Public.Web.Components.Server.Helpers
{
    public static class RequestHelpers
    {
        public static string GetClientIp(HttpRequestMessage request)
        {
            if (request != null)
            {
                if (request.Properties.ContainsKey("MS_HttpContext"))
                {
                    return ((System.Web.HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
                }
                if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
                {
                    var prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
                    return prop.Address;
                }
            }
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Request.UserHostAddress;
            }
            return null;
        }
    }
}
